package com.qf.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 约定,token会放在请求的参数中
 */
@Component
public class TokenInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String token = request.getParameter("token");

        if (isValidToken(token)) {
            return true;
        } else {
            System.out.println("token拦截器拦截到了非法token");
            System.out.println("token=" + token);
            System.out.println("IP=" + request.getRemoteAddr());

            throw new RuntimeException("token非法");
        }

//        return false;

    }

    private boolean isValidToken(String token) {
        return "aaa.bbb.ccc".equals(token);
    }
}
